package ru.spb.osll.myfit.client.view;

import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;

import java.util.HashMap;
import java.util.Map;

public class ViewLoader {
  /*
    Map: page name -> IViewController
    e.g. test_table -> TestTableViewController
   */
  private Map<String, IViewController> m_viewControllers = new HashMap<String, IViewController>();
  
  public ViewLoader(){
    m_viewControllers.put(ViewConstants.TEST_TABLE_ADDRESS, new TestTableViewController());
    m_viewControllers.put(ViewConstants.TEST_RESULT_ADDRESS, new TestResultViewController());
    m_viewControllers.put(ViewConstants.SUITE_ADDRESS, new SuiteViewController());
    m_viewControllers.put(ViewConstants.SUITE_RESULT_ADDRESS, new SuiteResultViewController());
    m_viewControllers.put(ViewConstants.SUITES_ADDRESS, new SuitesViewController());
    m_viewControllers.put(ViewConstants.CHART_COMPARISON_ADDRESS, new ChartComparisonViewController());
    m_viewControllers.put(ViewConstants.CHARTS, new ChartsViewController());
  }
  
  private IViewController m_defaultViewController = new MyFitViewController();

  public IView getView(String address) {
    String pageName = "";
    RegExp re = RegExp.compile("([a-zA-Z0-9_.]*)");
    if(re.test(address)){
      MatchResult m = re.exec(address);
      if (m.getGroupCount() > 0) {
        pageName = m.getGroup(0);
      }
    }
    if (m_viewControllers.containsKey(pageName)) {
      return m_viewControllers.get(pageName).getView(address);
    }
    return m_defaultViewController.getView(address);
  }
}
